

package com.seed.marketed.common.logtrack.intercept;

import java.lang.reflect.Method;
import java.util.UUID;

import com.seed.marketed.common.logtrack.constants.LogConstants;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;



@Aspect
@Component
public class ScheduleLogIntercept {
	
	private static final Logger logger = LoggerFactory.getLogger(ScheduleLogIntercept.class);

	@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
	public void annotationPoinCut() {
	}

	@Before("annotationPoinCut()")
	public void before(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		logger.info("{}方法前规则式拦截" , method.getName());
		MDC.put(LogConstants.TRACE_ID, UUID.randomUUID().toString().replace("-", ""));
	}

	@After("annotationPoinCut()")
	public void after(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		logger.info("{}方法后规则式拦截" , method.getName());
		MDC.remove(LogConstants.TRACE_ID);
	}
}
